home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / qdeck / sockets / tclient.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-11  |  5.1 KB  |  202 lines

  1. /*****************************************************************************
  2. *  
  3. *                                  DESQview/X 
  4. *                           BASIC STREAM DAEMON TESTER
  5. *
  6. *  The following code is used to test the basic stream daeomon (TDAEMON.C).
  7. *
  8. *
  9. ******************************************************************************/
  10.  
  11.  
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <sys\time.h>
  16.  
  17. #include <netdb.h>        
  18. #include <netinet\in.h>    
  19. #include <sys\socket.h>
  20. #include <sys\errno.h>
  21. #include <sys\ioctl.h>
  22.  
  23. #define TEST_SERVICE    "tcptest"
  24.  
  25. void    chat(int s);
  26.  
  27. extern int kbhit(void);
  28.  
  29. void main(int argc, char *argv[])
  30. {
  31.     int        s;
  32.     struct    sockaddr_in    addr;
  33.     struct    hostent        *h;
  34.     struct    servent        *serv;
  35.  
  36.     if(argc < 2){
  37.  
  38.         printf("\n\nUsage %s <host>\n\n",(argv[0] ? argv[0] : "tclient"));
  39.  
  40.         exit(1);
  41.     }
  42.  
  43.     /* Open a stream (TCP) socket for the conversation.            */
  44.  
  45.     s = socket(AF_INET,SOCK_STREAM,0);
  46.  
  47.     if(s < 0){
  48.  
  49.         printf("\nError:  Could not open stream socket.\n\n");
  50.  
  51.         exit(2);
  52.     }
  53.  
  54.     /* Retrieve the information about the port (service) that we    */
  55.     /* wish to connect to.                                                        */
  56.  
  57.     serv = getservbyname(TEST_SERVICE,"tcp");
  58.  
  59.     if(serv == NULL){
  60.  
  61.         printf("\nError:  Unable to retrieve information for %s service.\n\n",TEST_SERVICE);
  62.  
  63.         exit(3);
  64.     }
  65.  
  66.     /* Now get the host (address) information for the destination    */
  67.     /* host and fill the destination address structure giving it    */
  68.     /* the address from the host information and the port from        */
  69.     /* the retrieved service information.                                    */
  70.  
  71.     h = gethostbyname(argv[1]);
  72.  
  73.     if(h == NULL){
  74.  
  75.         printf("\nError:  Unknown host name specified.\n\n");
  76.  
  77.         exit(3);
  78.     }
  79.  
  80.     memset(&addr,0,sizeof(struct sockaddr_in));
  81.  
  82.     addr.sin_family    = AF_INET;
  83.         addr.sin_addr           = *((struct in_addr *)h->h_addr);
  84.     addr.sin_port        = serv->s_port;
  85.     
  86.     /* Attempt to connect to the destination host.                        */
  87.  
  88.     if(connect(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) != 0){
  89.  
  90.         printf("\nError:  Unable to connect to %s (%s).\n\n",argv[1],inet_ntoa(addr.sin_addr));
  91.  
  92.         exit(4);
  93.     }
  94.  
  95.     chat(s);
  96.  
  97.     so_close(s);
  98.  
  99.     exit(0);
  100. }
  101.  
  102.  
  103.  
  104. /*****************************************************************************
  105. *
  106. *    void    chat(int session);
  107. *
  108. *     The following code demonstrates some of the basic BSD4.3 socket I/O calls.
  109. *  Basically, we poll for data on the socket, while allowing the user to 
  110. *  enter some text which we then send to our connection partner.
  111. *
  112. ******************************************************************************/
  113. void    chat(int s)
  114. {
  115.     struct    timeval    t;                            /* set to 0 to indicate polling */
  116.     long        nonBlocking = 1l;                    
  117.     char        recvBuff[100];                        /* Buffer to receive data into */
  118.     char        sendBuff[100];                        /* Buffer data sent from */
  119.     long        ioBit,readBit,writeBit;            /* I/O bits for the socket */
  120.     int        bytes,i;                                
  121.     char        done;
  122.  
  123.     printf("\n\n-----------------------------------");
  124.     printf("---------------------------------------------\n");
  125.     printf("<Enter> - Send (blank line to end)\n\n");
  126.  
  127.     /*  Set the I/O strategy for the socket to non-blocking.  This will    */
  128.     /*  force I/O calls to return immediately if there is nothing to        */
  129.     /*     do.                                                                                    */
  130.        
  131.     ioctl(s,FIONBIO,(char *)&nonBlocking); 
  132.  
  133.     /*  Initialize the timeout for the select call to 0.  This will      */
  134.     /*  transform the call from a waiting call to a polling call.            */
  135.  
  136.     memset(&t,0,sizeof(struct timeval));
  137.  
  138.     /*  The select call takes pointers to bit arrays indicating which    */
  139.     /*  sockets we are concerned about.  Initialize the bit indicating   */
  140.     /*  the current socket.                                                                */
  141.  
  142.     ioBit = (long)(1l << s);
  143.  
  144.     done = 0;
  145.  
  146.     while(!done){
  147.  
  148.         writeBit = readBit = ioBit;    
  149.  
  150.         select(s + 1,(struct fd_set *)&readBit,(struct fd_set *)&writeBit,(struct fd_set *)NULL,&t);    /* Get read/write status */
  151.  
  152.         /* If the 'read' bit is still set after the select call, one of      */
  153.         /* two things is indicated.  Either the socket has been closed,     */
  154.         /* or there is data to read for the socket.                                 */
  155.  
  156.         if(readBit & ioBit){
  157.  
  158.             bytes = recv(s,recvBuff,sizeof(recvBuff),0);    
  159.  
  160.             switch(bytes){
  161.                 case 0:                                     /* Normal close    */
  162.                     printf("\n\nSession Terminated by Partner.\n\n");
  163.                     done = 1;
  164.                     break;
  165.                 case -1:                    
  166.                     if(errno != EWOULDBLOCK){        /* Abnormal close */
  167.                         printf("\n\nSession Terminated Abnormally on Read.\n\n");
  168.                         done = 1;
  169.                     }
  170.                     break;
  171.                 default:
  172.                     for(i = 0;i < bytes;i++)         /* Got some data    */
  173.                         printf("%c",recvBuff[i]);
  174.             }
  175.         } else {
  176.  
  177.         /* If the 'write' bit is set and a key has been hit, collect the    */
  178.         /* user data and send it to the connection partner.                    */
  179.  
  180.             if((writeBit & ioBit) && kbhit()){
  181.     
  182.                 strcpy(sendBuff,"<");
  183.                 gets(sendBuff + 1);
  184.  
  185.                 if(!strlen(sendBuff + 1)){
  186.                     done = 1;
  187.                     break;
  188.                 }
  189.                 strcat(sendBuff,">\n");
  190.     
  191.                 bytes = send(s,sendBuff,strlen(sendBuff),0);
  192.  
  193.                 if(bytes == -1 && errno != EWOULDBLOCK){        /* Error Occured */
  194.                     printf("\n\nSession Terminated Abnormally on Write.\n\n");
  195.                     done = 1;
  196.                 }
  197.             }
  198.         }        
  199.     }
  200. }
  201.  
  202.